home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / e_to_l / fbuilder / delphi / demos / vardlg.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  5.8 KB  |  252 lines

  1. { FormulaBuilder                }
  2. { YGB Software, Inc.            }
  3. { Copyright 1995 Clayton Collie }
  4. { All rights reserved           }
  5.  
  6. {* Simple Form & Unit to handle variables belonging to expressions *}
  7. {* Uses the Handle property and DLL level calls                    *}
  8. unit Vardlg;
  9. interface
  10. uses
  11.   FBCalc,
  12.   WinTypes, WinProcs, Classes, Graphics, Forms, Controls, Buttons,
  13.   StdCtrls, ExtCtrls, Grids;
  14.  
  15. type
  16.   TVariablesDlg = class(TForm)
  17.     OKBtn: TBitBtn;
  18.     CancelBtn: TBitBtn;
  19.     Bevel1: TBevel;
  20.     BitBtn1: TBitBtn;
  21.     BitBtn2: TBitBtn;
  22.     ValueEdit: TEdit;
  23.     Label1: TLabel;
  24.     StringGrid1: TStringGrid;
  25.     SpeedButton1: TSpeedButton;
  26.     procedure FormActivate(Sender: TObject);
  27.     procedure BitBtn1Click(Sender: TObject);
  28.     procedure StringGrid1SelectCell(Sender: TObject; Col, Row: Longint;
  29.       var CanSelect: Boolean);
  30.     procedure SpeedButton1Click(Sender: TObject);
  31.   private
  32.     { Private declarations }
  33.     varnames : TStringList;
  34.     fHandle : HEXPR;
  35.     Procedure Fillgrid;
  36.     Function  VariableCount : integer;
  37.     Procedure SetHandle( theHandle : HEXPR );
  38.   public
  39.     { Public declarations }
  40.     Property   Handle : HEXPR read fHandle write setHandle;
  41.   end;
  42.  
  43. var
  44.   VariablesDlg: TVariablesDlg;
  45.  
  46.   Procedure ManageVariables(handle : HEXPR);
  47.  
  48. implementation
  49. uses Sysutils,newVarfm;
  50.  
  51. {$R *.DFM}
  52.  
  53.  Procedure ManageVariables(handle : HEXPR);
  54.  var varfm : TVariablesDlg;
  55.  begin
  56.    Application.CreateForm(TVariablesDlg,VarFm);
  57.    varfm.Handle := Handle;
  58.    varfm.ShowModal;
  59.    varfm.Free;
  60.  end;
  61.  
  62.  
  63.  Function TypeToChar(vtype : longint):char;
  64.  begin
  65.    result := '?';
  66.    case vtype of
  67.       vtInteger : result := 'I';
  68.       vtFloat   : result := 'F';
  69.       vtString  : result := 'S';
  70.       vtBoolean : result := 'B';
  71.       vtDate    : result := 'D';
  72.       vtChar    : result := 'C';
  73.    end;
  74.  end;
  75.  
  76.  
  77.  Function CharToDataType(vt : char):datatypes;
  78.  begin
  79.    result := vtNone;
  80.    case vt of
  81.      'I' : result := vtInteger;
  82.      'F' : result := vtFloat;
  83.      'S' : result := vtString;
  84.      'B' : result := vtBoolean;
  85.      'D' : result := vtDate;
  86.      'C' : result := vtChar;
  87.    end;
  88.  end;
  89.  
  90.  
  91.  Procedure DisplayError( ErrTxt : PChar );
  92.  begin
  93.    Application.MessageBox(ErrTxt,'Error',MB_OK or MB_ICONHAND);
  94.  end;
  95.  
  96.  
  97.  Function EvaluateInfix(handle : HEXPR;s : String;var good : boolean):string;far;
  98.  var res    : integer;
  99.      outbuf : array[0..256] of char;
  100.      respch : PChar;
  101.  
  102.  begin
  103.    Result := '';
  104.    Good   := False;
  105.    if (s = '') then
  106.    begin
  107.      result := 'Invalid Expression!';
  108.      exit;
  109.    end;
  110.    s := s + #0;
  111.    res := FBSetExpression(handle,@s[1]);
  112.    respCh := @outbuf;
  113.  if res = EXPR_SUCCESS then
  114.    begin
  115.      res := FBEvaluate(Handle,respch,sizeof(outbuf));
  116.      if res = EXPR_SUCCESS then
  117.         Result := strpas(respch);
  118.    end;
  119.    if res = EXPR_SUCCESS then
  120.       good := true
  121.     else
  122.       begin
  123.         FBGetErrorString(res,respch,sizeof(outbuf)-1);
  124.         DisplayError(respch);
  125.       end;
  126. end;
  127.  
  128.  
  129.  
  130.  
  131. Procedure TVariablesDlg.SetHandle( theHandle : HEXPR );
  132. begin
  133.   fHandle := theHandle;
  134.   FillGrid;
  135. end;
  136.  
  137. procedure TVariablesDlg.FormActivate(Sender: TObject);
  138. begin
  139.   FillGrid;
  140. end;
  141.  
  142. Function  TVariablesDlg.VariableCount : integer;
  143. begin
  144.   result := FBGetVariableCount(fHandle);
  145. end;
  146.  
  147.  
  148. Procedure TvariablesDlg.FillGrid;
  149. var i,cnt : integer;
  150.     v      : TValueRec;
  151.     nom    : string[55];
  152.     vname  : pchar;
  153.     varcnt : integer;
  154.     buf    : array[0..75] of char;
  155.     list   : TStringList;
  156.     typ    : longint;
  157.  
  158. begin
  159.   StringGrid1.Cells[0,0] := '#';
  160.   StringGrid1.Cells[1,0] := 'NAME';
  161.   StringGrid1.Cells[2,0] := 'TYPE';
  162.   Cnt    := 0;
  163.   varcnt := VariableCount;
  164.   if varcnt < 1 then exit;
  165.   StringGrid1.Rowcount := varcnt + 1;
  166.   vname := @buf;
  167.   List := TStringList.Create;
  168.   TRY
  169.     For i := 0 to VarCnt-1 do
  170.     begin
  171.       FBPeekVariable(fHandle,i,vname,sizeof(buf)-1,v);
  172.       typ := v.vtype;
  173.       List.AddObject(strpas(vname), pointer(typ) );
  174.     end;
  175.     List.Sorted := True;
  176.     With stringGrid1 do
  177.     For i := 0 to list.count-1 do
  178.     begin
  179.       FBPeekVariable(fHandle,i,vname,sizeof(buf)-1,v);
  180.       inc(cnt);
  181.       Cells[0,cnt] := inttostr(cnt);
  182.       Cells[1,cnt] := List.Strings[i];
  183.       Cells[2,cnt] := TypeToChar( longint(List.Objects[i]) );
  184.      { type }
  185.     end;
  186.   FINALLY
  187.     List.Free;
  188.   END;
  189.   StringGrid1.Invalidate;
  190.   StringGrid1.Refresh;
  191.  end;
  192.  
  193. procedure TVariablesDlg.BitBtn1Click(Sender: TObject);
  194. begin
  195.   if NEWVARFM.AddNewVariable(fHandle) then
  196.      FillGrid;
  197. end;
  198.  
  199. procedure TVariablesDlg.StringGrid1SelectCell(Sender: TObject; Col,
  200.   Row: Longint; var CanSelect: Boolean);
  201. var vname  : string[60];
  202.     _value : array[0..256] of char;
  203.     val    : pchar;
  204.     v      : TValueRec;
  205.  
  206. begin
  207.   if col <> 1 then exit;
  208.   vname := StringGrid1.Cells[1,Row]+#0;
  209.   val   := @_value;
  210.   if FBGetVarAsString(fHandle,@vname[1],val,sizeof(_Value)) = EXPR_SUCCESS then
  211.   begin
  212.      ValueEdit.Text := Strpas(val);
  213.   end;
  214. end;
  215.  
  216.  
  217. procedure TVariablesDlg.SpeedButton1Click(Sender: TObject);
  218. var t       : string;
  219.     v       : TValueRec;
  220.     good    : boolean;
  221.     typech  : String[1];
  222.     typ     : datatypes;
  223.     vname   : string[45];
  224.     tmp     : string;
  225.  
  226. begin
  227.   if (StringGrid1.row = 0) or (VariableCount < 1) then exit;
  228.   t := valueEdit.Text;
  229.   if T = '' then
  230.   begin
  231.      DisplayError('No value specified');
  232.      exit;
  233.   end;
  234. (*  v := EvaluateInfix(t,good);
  235.   if not good then exit; *)
  236.   with stringGrid1 do
  237.   begin
  238.     typech := Cells[2,Row];
  239.     vname  := Cells[1,Row];
  240.   end;
  241.   fillchar(v,sizeof(v),0);
  242.   typ := CharToDatatype(typech[1]);
  243.   tmp := valueEdit.Text;
  244.   if typ = vtString then
  245.       tmp := '"'+tmp+'"';
  246.   tmp := Vname + ' := '+ tmp;
  247.   t := EvaluateInfix(fhandle,tmp,Good);
  248.  { FillGrid; }
  249. end;
  250.  
  251. end.
  252.